<?php
namespace App\Utilitys;

class TxMap
{
    //腾讯地图
    //康友圈 FB4BZ-67UK3-EA23R-RUD3Q-AHOMT-FZFD7
    protected $mapKey = 'ZY5BZ-66OCU-4WYVL-GT6II-CWJG3-EWBDJ';

    /**
     * 腾讯地图，坐标转地址
     * https://apis.map.qq.com/ws/geocoder/v1/?location=
     * @param string $map 坐标
     */
    public function mapByLocate($map)
    {
        $httpUrl = 'https://apis.map.qq.com/ws/geocoder/v1/';
        $httpUrl .= '?key='.$this->mapKey;
        $httpUrl .= '&location='.$map;
        $res = $this->toCurl($httpUrl);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        return $this->toJson(0, '', $res);
    }

    /**
     * 腾讯地图，距离计算
     * https://apis.map.qq.com/ws/distance/v1/matrix
     * @param string $from 坐标1
     * @param string $to 坐标2
     * @param string $mode出行方式：walking步行，bicycling骑行，driving驾车
     */
    public function mapByDistance($from, $to, $mode = 'walking')
    {
        $httpUrl = 'https://apis.map.qq.com/ws/distance/v1/matrix';
        $httpUrl .= '?key='.$this->mapKey;
        $httpUrl .= '&mode=walking';
        $httpUrl .= '&from='.$from;
        $httpUrl .= '&to='.$to;
        $httpUrl .= '&mode='.$mode;
        $res = $this->toCurl($httpUrl);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        if (!isset($res['rows'][0]['elements'][0]['distance'])
            || !$res['rows'][0]['elements'][0]['distance']) {
            return $this->toJson(-1, '地图接口错误');
        }
        $res = $res['rows'][0]['elements'][0]['distance'].'米';
        return $this->toJson(0, '', $res);
    }

    /**
     * 接口返回
     */
    public function toJson($code = 0, $msg = '', $data = [])
    {
        if ($code == -1) $code = -101;
        if ($code == -2) $code = -102;
        if ($code == 0) $code = 200;
        if (!$msg) $msg = 'success';
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
    }

    /**
     * 构建CURL请求，单线程
     * @param $url 请求地址
     * @param $string 参数拼接的字符串
     * @param $method 请求方式：put、post、get、delete
     * @return $json
     */
    public function toCurl($url)
    {
        $curl = curl_init(); //初始化
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        $output = curl_exec($curl);
        curl_close($curl);
        if (!$output) {
            return array('code'=>-1, 'msg'=>'请求失败');
        }
        $res = json_decode($output,true);
        //ddd($res);
        if ($res['status'] != 0) {
            return array('code' => -2, 'msg' => $res['message']);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res['result']);
    }
}